package dfl.bysj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.regexp.internal.RE;
import dfl.bysj.config.RedisKey;
import dfl.bysj.config.model.VO.UserDto;
import dfl.bysj.controller.push.SendMessage;
import dfl.bysj.entity.*;
import dfl.bysj.entity.VO.ReserveSUCCVO;
import dfl.bysj.entity.VO.ReserveVO;
import dfl.bysj.result.R;
import dfl.bysj.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author DFL
 * @since 2022-04-26
 */
@Slf4j
@RestController
@RequestMapping("/studentSeatRelation")
public class StudentSeatRelationController {
    StudentSeatRelationService studentSeatRelationService;


    private SeatStateService seatStateService;
    private SendMessage sendMessage;

    private FloorService floorService;
    private SeatService seatService;

    private UserTeacherStudentService userTeacherStudentService;
    private RedisTemplate redisTemplate;


    @Autowired
    public void setUserTeacherStudentService(UserTeacherStudentService userTeacherStudentService) {
        this.userTeacherStudentService = userTeacherStudentService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    public void setFloorService(FloorService floorService) {
        this.floorService = floorService;
    }

    @Autowired
    public void setSeatService(SeatService seatService) {
        this.seatService = seatService;
    }

    @Autowired
    public void setSeatStateService(SeatStateService seatStateService) {
        this.seatStateService = seatStateService;
    }

    @Autowired
    public void setSendMessage(SendMessage sendMessage) {
        this.sendMessage = sendMessage;
    }

    @Autowired
    public void setStudentSeatRelationService(StudentSeatRelationService studentSeatRelationService) {
        this.studentSeatRelationService = studentSeatRelationService;
    }

    @GetMapping("/main/reserve")
    public R main_reserve(@RequestHeader("Authorization") String token) {
        Map<String, Object> map = new HashMap<>();
        UserDto userDto= (UserDto) redisTemplate.opsForValue().get(RedisKey.TOKEN+token);
        UserTeacherStudent userTeacherStudent=userTeacherStudentService.getOne(new QueryWrapper<UserTeacherStudent>()
                .eq("stu_id", userDto.getStudent().getStuId())
                .eq("use_id", userDto.getUser().getId())
                .eq("check_state",true));

        if (userTeacherStudent!=null){
            List<StudentSeatRelation> studentSeatRelationList=studentSeatRelationService.getAllSeatStateByStuUseId(new QueryWrapper<StudentSeatRelation>()
                    .eq("stu_use_id", userTeacherStudent.getStuUseId())
                    .eq("overtime_check", false)
                    .orderByDesc("update_time"));
            ReserveSUCCVO reserveSUCCVO = ReserveSUCCVO.getReserveSUCCVO(studentSeatRelationList.get(0));

            SeatState seatState = seatStateService.getById(reserveSUCCVO.getSeaStaId());
            Seat seat = seatService.getSeaNumBySeaId(seatState.getSeaId());
            Floor floor = floorService.getFloorByfloId(seat.getFloId());
            map.put("reserve", reserveSUCCVO);
            map.put("seaNum", seat.getSeaNum());
            map.put("floNum", floor.getFloNum());

            return R.ok().data(map);

        }
        return R.error();
//        log.info(reserveSUCCVO.toString());


    }

    @GetMapping("/reserve")
    public R reserve(ReserveVO reserveVO, @RequestHeader("Authorization") String token) {
        log.info(reserveVO.toString());
        log.info("token:" + token);
        ReserveSUCCVO reserveSUCCVO = studentSeatRelationService.reserve(reserveVO);
        sendMessage.pushReserveSUCC(token, reserveSUCCVO);
        return R.ok().data("reserve", reserveSUCCVO);
    }


    @GetMapping("/cancel")
    public R cancel(String stuSeaId, String stuUseId) {
        return R.ok().data("honestGrade", studentSeatRelationService.cancelSign(Integer.parseInt(stuSeaId), Integer.parseInt(stuUseId)));
    }

    @GetMapping("/sign")
    public R sign(String seaId, String stuSeaId) {
        studentSeatRelationService.sign(seaId, stuSeaId);
        return R.ok();
    }

    @GetMapping("/signback")
    public R signback(String seaId, String stuSeaId) throws InterruptedException {
        studentSeatRelationService.signback(seaId, stuSeaId);
        return R.ok();
    }

    @GetMapping("/list")
    public R list(String stuUseId) {
        return R.ok().data("list", studentSeatRelationService.getAllReserveSUCCByStuSeaId(stuUseId));
    }

    @GetMapping("/seaStaId")
    public R seaSeat(String seaStaId, String resStartTime) {
        return R.ok().data("reserveList", studentSeatRelationService.getReserveSUCCBySeaStaId(seaStaId, resStartTime)
        );
    }

    @GetMapping("/stuId/list")
    public R stuId_list(String stuId) {
        return R.ok().data("list", studentSeatRelationService.getAllReserveSUCCByStuId(Integer.parseInt(stuId)));
    }

    @GetMapping("/powerCancel")
    public R powerCancel(String stuUseId) throws InterruptedException {
        studentSeatRelationService.powerCancel(stuUseId);
        return R.ok();
    }
}


